
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;

using System.Linq.Dynamic.Core;
 using Microsoft.EntityFrameworkCore; 

using DualControl.RiskPoints.Authorization;
using DualControl.RiskPoints.Dtos;
using DualControl.RiskPoints;

namespace DualControl.RiskPoints
{
    /// <summary>
    /// RiskPoint应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize(RiskPointAppPermissions.RiskPoint)]
    public class RiskPointAppService : DualControlAppServiceBase, IRiskPointAppService
    {
        private readonly IRepository<RiskPoint, int> _riskpointRepository;


        private readonly IRiskPointManager _riskpointManager;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public RiskPointAppService(IRepository<RiskPoint, int>riskpointRepository, IRiskPointManager riskpointManager)
        {
            _riskpointRepository = riskpointRepository;
            _riskpointManager = riskpointManager;
        }


        /// <summary>
        /// 获取RiskPoint的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<RiskPointListDto>> GetPagedRiskPoints(GetRiskPointsInput input)
        {
            var query = _riskpointRepository.GetAll();
            // TODO:根据传入的参数添加过滤条件

            var riskpointCount = await query.CountAsync();

            var riskpoints = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            // var riskpointListDtos = ObjectMapper.Map<List <RiskPointListDto>>(riskpoints);
            var riskpointListDtos = riskpoints.MapTo<List<RiskPointListDto>>();

            return new PagedResultDto<RiskPointListDto>(riskpointCount,riskpointListDtos);
        }


        /// <summary>
        /// 通过指定id获取RiskPointListDto信息
        /// </summary>
        public async Task<RiskPointListDto> GetRiskPointByIdAsync(EntityDto<int> input)
        {
            var entity = await _riskpointRepository.GetAsync(input.Id);

            return entity.MapTo<RiskPointListDto>();
        }

        /// <summary>
        /// MPA版本才会用到的方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<GetRiskPointForEditOutput> GetRiskPointForEdit(NullableIdDto<int> input)
        {
            var output = new GetRiskPointForEditOutput();
            RiskPointEditDto riskpointEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _riskpointRepository.GetAsync(input.Id.Value);

                riskpointEditDto = entity.MapTo<RiskPointEditDto>();

                //riskpointEditDto = ObjectMapper.Map<List <riskpointEditDto>>(entity);
            }
            else
            {
                riskpointEditDto = new RiskPointEditDto();
            }

            output.RiskPoint = riskpointEditDto;
            return output;
        }


        /// <summary>
        /// 添加或者修改RiskPoint的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrUpdateRiskPoint(CreateOrUpdateRiskPointInput input)
        {

            if (input.RiskPoint.Id.HasValue)
            {
                await UpdateRiskPointAsync(input.RiskPoint);
            }
            else
            {
                await CreateRiskPointAsync(input.RiskPoint);
            }
        }


        /// <summary>
        /// 新增RiskPoint
        /// </summary>
       // [AbpAuthorize(RiskPointAppPermissions.RiskPoint_Create)]
        protected virtual async Task<RiskPointEditDto> CreateRiskPointAsync(RiskPointEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<RiskPoint>(input);

            entity = await _riskpointRepository.InsertAsync(entity);
            return entity.MapTo<RiskPointEditDto>();
        }

        /// <summary>
        /// 编辑RiskPoint
        /// </summary>
        //[AbpAuthorize(RiskPointAppPermissions.RiskPoint_Edit)]
        protected virtual async Task UpdateRiskPointAsync(RiskPointEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _riskpointRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            // ObjectMapper.Map(input, entity);
            await _riskpointRepository.UpdateAsync(entity);
        }



        /// <summary>
        /// 删除RiskPoint信息的方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
       // [AbpAuthorize(RiskPointAppPermissions.RiskPoint_Delete)]
        public async Task DeleteRiskPoint(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _riskpointRepository.DeleteAsync(input.Id);
        }



        /// <summary>
        /// 批量删除RiskPoint的方法
        /// </summary>
        //[AbpAuthorize(RiskPointAppPermissions.RiskPoint_BatchDelete)]
        public async Task BatchDeleteRiskPointsAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _riskpointRepository.DeleteAsync(s => input.Contains(s.Id));
        }


        /// <summary>
        /// 导出RiskPoint为excel表,等待开发。
        /// </summary>
        /// <returns></returns>
        //public async Task<FileDto> GetRiskPointsToExcel()
        //{
        //	var users = await UserManager.Users.ToListAsync();
        //	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
        //	await FillRoleNames(userListDtos);
        //	return _userListExcelExporter.ExportToFile(userListDtos);
        //}



        //// custom codes

        //// custom codes end

    }
}


